package org.jeecg.modules.fk.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.DivisorTypeEnum;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.fk.entity.FactorPoolListVo;
import org.jeecg.modules.fk.entity.IndexManage;
import org.jeecg.modules.fk.service.IFactorPoolService;
import org.jeecg.modules.fk.service.IIndexManageService;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.fk.service.IIndexSetService;
import org.jeecg.modules.qyxx.service.IFkPdfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: pointer_manage
 * @Author: ZJJ
 * @Date: 2021-05-06
 * @Version: V1.0
 */
@Api(tags = "指标管理")
@RestController
@RequestMapping("/IndexManage/indexManage")
@Slf4j
public class IndexManageController extends JeecgController<IndexManage, IIndexManageService> {
    @Autowired
    private IIndexManageService indexManageService;

    @Autowired
    private IFactorPoolService factorPoolService;

    @Autowired
    private IIndexSetService indexSetService;

    /**
     * 分页列表查询
     *
     * @param indexManage
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "指标管理分页列表查询")
    @ApiOperation(value = "指标管理分页列表查询", notes = "指标管理分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(IndexManage indexManage,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<IndexManage> queryWrapper = QueryGenerator.initQueryWrapper(indexManage, req.getParameterMap());
        Page<IndexManage> page = new Page<IndexManage>(pageNo, pageSize);
        IPage<IndexManage> pageList = indexManageService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "查询所有指标")
    @ApiOperation(value = "查询所有指标", notes = "查询所有指标")
    @GetMapping(value = "/listAll")
    public Result listAll(){
        return Result.ok(indexManageService.list());
    }

    /**
     * 通过指标集ID获取指标列表
     */
    @AutoLog(value = "通过指标集ID获取指标列表")
    @ApiOperation(value = "通过指标集ID获取指标列表", notes = "通过指标集ID获取指标列表")
    @GetMapping(value = "/queryIndexListByIds")
    public Result<?> queryIndexListByIds(@RequestParam(name = "id") Long id) {
        List<Long> ids = indexSetService.queryIndexIds(id);
        if (ids == null || ids.size() <= 0) {
            return Result.error("该指标集没有指标");
        }
        return Result.OK(indexManageService.queryIndexListByIds(ids));
    }

    /**
     * 添加
     *
     * @param indexManage
     * @return
     */
    @AutoLog(value = "pointer_manage-添加")
    @ApiOperation(value = "pointer_manage-添加", notes = "pointer_manage-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody IndexManage indexManage) {
        indexManageService.save(indexManage);
        return Result.OK("添加成功！");
    }

    /**
     * 获取新增或者编辑的下拉框数据
     *
     * @param
     * @return
     */
    @PostMapping("/getList")
    public Result<?> getList() {
        DivisorTypeEnum[] enums = DivisorTypeEnum.values();
        List<FactorPoolListVo> list = new ArrayList<>();
        for (DivisorTypeEnum anEnum : enums) {
            FactorPoolListVo vo = new FactorPoolListVo();
            vo.setFactorTypeName(anEnum.getName());
            vo.setMenuList(factorPoolService.getFactorSourceList(anEnum.getValue()));
            list.add(vo);
        }
        FactorPoolListVo vo = new FactorPoolListVo();
        vo.setList(list);
        vo.setDeclarationFormList(factorPoolService.getFactorNameList());
        return Result.OK(vo);
    }


    /**
     * 编辑
     *
     * @param indexManage
     * @return
     */
    @AutoLog(value = "pointer_manage-编辑")
    @ApiOperation(value = "pointer_manage-编辑", notes = "pointer_manage-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody IndexManage indexManage) {
        indexManageService.updateById(indexManage);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "pointer_manage-通过id删除")
    @ApiOperation(value = "pointer_manage-通过id删除", notes = "pointer_manage-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        boolean flag = indexManageService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "pointer_manage-批量删除")
    @ApiOperation(value = "pointer_manage-批量删除", notes = "pointer_manage-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.indexManageService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        IndexManage indexManage = indexManageService.getById(id);
        if (indexManage == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(indexManage);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param indexManage
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, IndexManage indexManage) {
        return super.exportXls(request, indexManage, IndexManage.class, "pointer_manage");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, IndexManage.class);
    }

}
